package com.xctech.ipb.dpn.hyc.listener;

import com.xctech.ipb.dpn.common.utils.JsonUtils;
import com.xctech.ipb.dpn.hyc.constant.TopicConstants;
import com.xctech.ipb.dpn.hyc.domain.entity.Order;
import com.xctech.ipb.dpn.hyc.domain.entity.OrderPower;
import com.xctech.ipb.dpn.hyc.domain.service.OrderService;
import com.xctech.ipb.dpn.hyc.domain.service.OrderPowerService;
import com.xctech.ipb.dpn.hyc.model.req.OrderInfoReq;
import com.xctech.ipb.dpn.hyc.model.req.OrderReq;
import com.xctech.ipb.dpn.hyc.model.req.PowerDetailsReq;
import com.xctech.ipb.dpn.hyc.model.req.SyncOrderDelReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderListener {

    private static final Logger log = LoggerFactory.getLogger(OrderListener.class);

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderPowerService orderPowerService;

    @KafkaListener(topics = TopicConstants.TOPIC_ORDER_SYNC, groupId = "rwa-dpn-group")
    public void listen(String message) {

        try {
            log.info("同步订单信息-开始");
            //还原body体
            OrderReq req = JsonUtils.fromJson(message, OrderReq.class);
            log.info("同步订单信息-消息体: {}", req);
            
            // 转换请求对象为实体对象
            List<Order> orderList = new ArrayList<>();
            List<OrderPower> powerList = new ArrayList<>();
            
            for (OrderInfoReq orderInfoReq : req.getOrderInfoList()) {
                // 转换订单信息
                Order order = new Order();
                BeanUtils.copyProperties(orderInfoReq, order);
                orderList.add(order);
                
                // 转换功率详情信息
                if (orderInfoReq.getPowerDetails() != null) {
                    for (PowerDetailsReq powerDetailsReq : orderInfoReq.getPowerDetails()) {
                        OrderPower orderPower = new OrderPower();
                        BeanUtils.copyProperties(powerDetailsReq, orderPower);
                        orderPower.setOrderNum(order.getOrderNum());
                        powerList.add(orderPower);
                    }
                }
            }
            
            // 批量插入或更新订单
            int orderCount = orderService.insertOrUpdateBatch(orderList);
            
            // 批量插入或更新功率详情
            int powerCount = orderPowerService.insertOrUpdateBatch(powerList);
            
            log.info("同步订单信息结束-成功: 订单{}条，功率详情{}条", orderCount, powerCount);

        } catch (Exception e) {

            log.error("同步订单信息结束-失败: {}", e.getMessage());

        }

    }

    @KafkaListener(topics = TopicConstants.TOPIC_ORDER_SYNC_DEL, groupId = "rwa-dpn-group")
    public void listenOrderDel(String message) {

        try {
            log.info("订单删除信息-开始");
            //还原body体
            SyncOrderDelReq req = JsonUtils.fromJson(message, SyncOrderDelReq.class);
            log.info("订单删除信息-消息体: {}", req);
            //获取订单编号集合
            List<String> orderNumList = req.getOrderInfoList().stream().map(SyncOrderDelReq.OrderDelInfo::getOrderNum).toList();
            orderService.deleteByOrderNumList(orderNumList);
            log.info("订单删除信息-成功");
        } catch (Exception e) {
            log.info("订单删除信息-失败:{}", e.getMessage());
        }

    }
} 